<template>
  <LevelAssignEditor ref="refAssignTree" :comment="props.comment" :expand-all="true"
                   :name="'角色'" :blank-tip="blankTip" :load-asset-data="loadRoleData"
                   :load-hold-asset="loadHoldAsset" :load-hold-other-asset="loadHoldOtherAsset"
                   :entry="entry" :readonly="props.readonly" :assign-assets="assignRole" :product="props.product" />
</template>

<script setup lang="ts">

import {getCurrentInstance, nextTick, onMounted, onUnmounted, ref, toRaw} from "vue";
import {useLoadAssign} from "@/utils/sop/UseLoadAssign";
import {loadAssetLevel} from "@/apis/sop";
import {currentNames, TYPES} from "@/stores/conf";
import {useStore} from "@/stores";
import {handleLevelData} from "@/utils/SystemUtil";
import {refreshUserAssets} from "@/utils/sop/RoleUtil";
import LevelAssignEditor from "@/views/sop/assign/LevelAssignEditor.vue";

const store = useStore();
const refAssignTree = ref();
const showAllRole = ref(true);
let otherOwnerRole: any = [];

const props = withDefaults(defineProps<{
  entry: any,
  readonly?: boolean,
  comment?: string,
  product?: boolean,
}>(), {});

const blankTip = "当前平台还未分配任何角色,请点击下方按钮分配角色.";

const {proxy} = getCurrentInstance();
const {load, assign} = useLoadAssign(props.entry);

const getTargetId = () => {
  return '';
}

const productCode = () => {
  return refAssignTree?.value.getProductCode();
}

const refresh = async () => {
  await refAssignTree.value?.refresh();
}

const handleOtherOwnerRole = (roles: any) => {
  if (roles)
    roles.forEach((item: any) => {
      if (item.brief && item.brief.ownerType == 2) {
        otherOwnerRole.push(toRaw(item.brief));
      }
      if (item.children && item.children.length > 0) {
        handleOtherOwnerRole(item.children);
      }
    });
}

const loadRoleData = async (params: any) => {
  otherOwnerRole = [];
  const current: any = store.getCurrent(props.entry.name);
  if (props.entry.name == currentNames.PLAT_USER &&
      current.item.detail && current.item.detail.trees) {
    handleOtherOwnerRole(current.item.detail.trees[TYPES.ROLE]);
  }

  const platform: any = store.getCurrentPlatform();
  const result = await loadAssetLevel({
    server: props.entry.server,
    targetId: platform.id,
    assetType: TYPES.ROLE,
    ...params,
    productCode: productCode(),
  });

  if (result && result.data) {
    const menuData: any = [];
    handleLevelData(menuData, result.data, []);
    delete result.data;
    result.data = menuData;
  }

  return result;
}

const loadHoldAsset = async () => {
  let holdItems = [];
  const selAssets = await load(getTargetId(), TYPES.ROLE, {
    current: 1,
    pageSize: 9999,
    productCode: productCode(),
  });

  if (selAssets && selAssets.code === 0 && selAssets.data) {
    holdItems = toRaw(selAssets.data.data) || [];
  } else {
    holdItems = [];
  }

  return holdItems;
}

const loadHoldOtherAsset = async () => {
  return showAllRole.value ? otherOwnerRole : [];
}

const refreshUserMenus = async () => {
  if (await refreshUserAssets(currentNames.PLAT_USER)) {
    proxy.$bus.emit('roleChanged');
  }
}

const assignRole = async (assets: any) => {
  const result: any = await assign(getTargetId(), TYPES.ROLE, assets, productCode(), 1);
  setTimeout(() => refreshUserMenus(),300);
  return result;
}

onMounted(() => {
  if (props.entry.name == currentNames.PLAT_USER) {
    proxy.$bus.on('roleChanged', () => {
      refresh();
    });
  }
})

onUnmounted(() => {
  if (props.entry.name == currentNames.PLAT_USER) {
    proxy.$bus.off('roleChanged');
  }
})

defineExpose({
  refresh,
})

</script>

<style scoped>
.el-form-item-class .el-form-item__label {
  font-size: 12px;
}

</style>
